# TrayMenu.py
# Copyright (C) otacon 2010 <orfeo18@gmail.com>
# 
# AdSense-Monitor is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# AdSense-Monitor is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import gobject

class TrayMenu(gobject.GObject):
    """
    a widget that represents the menu displayed on the trayicon in online mode
    """
    __gsignals__ = {
    'open-preferences' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    'open-login' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    'update-adsense' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    'open-about' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    'go-to-website' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    'quit' :
            (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                ()),
    }
    
    def __init__(self):
        """
        constructor

        """
        gobject.GObject.__init__(self)
        self.menu = gtk.Menu()
        self.quit = gtk.ImageMenuItem(gtk.STOCK_QUIT)
        self.quit.connect('activate',  self.on_item_clicked )

        self.login = gtk.ImageMenuItem("Login as...")
        icon = gtk.Image()
        icon.set_from_stock(gtk.STOCK_CONNECT,gtk.ICON_SIZE_MENU)
        self.login.set_image(icon)
        self.login.connect('activate', self.on_item_clicked )

        self.preferences = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
        self.preferences.connect('activate', self.on_item_clicked )

        self.refresh = gtk.ImageMenuItem(gtk.STOCK_REFRESH)
        self.refresh.connect('activate', self.on_item_clicked)

        self.about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
        self.about.connect('activate',self.on_item_clicked)
        
        self.website = gtk.ImageMenuItem("Check for Adsense-Monitor updates")
        icon = gtk.Image()
        icon.set_from_stock(gtk.STOCK_HOME,gtk.ICON_SIZE_MENU)
        self.website.connect('activate',self.on_item_clicked)

        self.menu.append(self.refresh)
        self.menu.append(gtk.SeparatorMenuItem())
        self.menu.append(self.login)
        self.menu.append(self.preferences)
        self.menu.append(gtk.SeparatorMenuItem())
        self.menu.append(self.about)
        self.menu.append(self.website)
        self.menu.append(gtk.SeparatorMenuItem())
        self.menu.append(self.quit)

    def get_menu(self):
        return self.menu

    def on_item_clicked(self, item):
        if item is self.preferences:
            self.emit('open-preferences')
        if item is self.login:
            self.emit('open-login')
        elif item is self.refresh:
            self.emit('update-adsense')
        elif item is self.about:
            self.emit('open-about')
        elif item is self.website:
            self.emit('go-to-website')
        elif item is self.quit:
            self.emit('quit')
            
    def refresh_enabled(self,enabled):
        self.refresh.set_sensitive(enabled)
